package com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.StudentList;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tnot.R;
public class StudentListAddStudentFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    View view;
    ImageView back;
    TextView tv_complete;
    EditText et_student_id,et_name,et_sex,et_class,et_score;
    LinearLayout ll_select_status;


    public StudentListAddStudentFragment() {
    }
    public static StudentListAddStudentFragment newInstance(String param1, String param2) {
        StudentListAddStudentFragment fragment = new StudentListAddStudentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.course_student_list_add_student_fragment, container, false);

        tv_complete = view.findViewById(R.id.tv_complete);
        tv_complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 验证所有输入字段都不为空
                if (validateInput()) {
                    // 收集用户输入的数据
                    Bundle result = new Bundle();
                    result.putString("student_id", et_student_id.getText().toString().trim());
                    result.putString("name", et_name.getText().toString().trim());
                    result.putString("sex", et_sex.getText().toString().trim());
                    result.putString("class", et_class.getText().toString().trim());
                    result.putString("score", et_score.getText().toString().trim());
                    // 设置返回结果
                    getParentFragmentManager().setFragmentResult("add_student_request", result);

                    NavController navController = Navigation.findNavController(v);
                    navController.navigateUp();
                }
            }
        });
        et_student_id = view.findViewById(R.id.et_student_id);
        et_name = view.findViewById(R.id.et_name);
        et_sex = view.findViewById(R.id.et_sex);
        et_class = view.findViewById(R.id.et_class);
        et_score = view.findViewById(R.id.et_score);


        back = view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigateUp();

            }
        });
        return view;
    }
    // 添加输入验证方法
    private boolean validateInput() {
        String studentId = et_student_id.getText().toString().trim();
        String name = et_name.getText().toString().trim();
        String sex = et_sex.getText().toString().trim();
        String className = et_class.getText().toString().trim();
        String score = et_score.getText().toString().trim();

        if (studentId.isEmpty()) {
            et_student_id.setError("学号不能为空");
            et_student_id.requestFocus();
            return false;
        }

        if (name.isEmpty()) {
            et_name.setError("姓名不能为空");
            et_name.requestFocus();
            return false;
        }

        if (sex.isEmpty()) {
            et_sex.setError("性别不能为空");
            et_sex.requestFocus();
            return false;
        }

        if (className.isEmpty()) {
            et_class.setError("班级不能为空");
            et_class.requestFocus();
            return false;
        }

        if (score.isEmpty()) {
            et_score.setError("成绩不能为空");
            et_score.requestFocus();
            return false;
        }

        // 验证成绩是否为有效数字
        try {
            int scoreValue = Integer.parseInt(score);
            if (scoreValue < 0 || scoreValue > 100) {
                et_score.setError("成绩应在0-100之间");
                et_score.requestFocus();
                return false;
            }
        } catch (NumberFormatException e) {
            et_score.setError("请输入有效的数字");
            et_score.requestFocus();
            return false;
        }

        return true;
    }
}